import { Storage } from '@/utils'
import { login } from '@/api/login'

export default {
  // 登录状态
  state: {
    token: Storage('TOKEN'),
    redirectRoute: Storage('REDIRECTROUTE'),
    userInfo: Storage('SNSAPIBASE')
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
      Storage('TOKEN', state.token)
    },
    SET_REDIRECTROUTE: (state, redirect) => {
      state.redirectRoute = redirect
      Storage('REDIRECTROUTE', state.redirectRoute)
    },
    SET_USERINFO: (state, userInfo) => {
        state.userInfo = userInfo
        Storage('USERINFO', state.userInfo)
      },
    LOGIN_OUT: state => {
      state.token = ''
      Storage(false)
    }
  },
  actions: {
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo) 
          .then(response => {
           const data = response.data
           commit('SET_TOKEN', data.token)
            resolve(response )
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}